﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ReferralTracker.Data.Entities;
using ReferralTracker.Data.Repositories;

namespace ReferralTracker.BusinessObjects {
    public class Administration : BusinessObject {

        public Administration() : base () {}
        public Administration(ISession session) : base (session) {}

        public void RegisterSite(ReferralSite site) {
            var refSiteRepo = new ReferralSiteRepository(_context);

            if (!refSiteRepo.SiteExists(site.RootUrl)) {
                refSiteRepo.Add(site);
                SaveChanges();
            }
            else {
                throw new Exception("RootUrl already exists in the database");
            }
        }

        public IList<ReferralSite> GetAllSites() {
            var refSiteRepo = new ReferralSiteRepository(_context);

            return refSiteRepo.SelectAll();
        }

        public ReferralSite GetSite(int Id) {
            var refSiteRepo = new ReferralSiteRepository(_context);

            return refSiteRepo.FindByKey(Id);
        }

        public ReferralSite GetSite(string url) {
            var refSiteRepo = new ReferralSiteRepository(_context);

            return refSiteRepo.FindByRootUrl(url);
        }

        public void DeleteSite(int Id) {
            var refSiteRepo = new ReferralSiteRepository(_context);
            var site = refSiteRepo.FindByKey(Id);

            refSiteRepo.Delete(site);
            SaveChanges();
        }
    }
}
